/**
 * 联系人相关接口
 * 
 */
const express = require("express")
const router = express.Router()
const pool = require("../../db/db.js")
const Response = require("../../utils/ResponseData.js")
// 新增联系人
router.post("/add", (req, res, next) => {
    // let status = false //设置修改是否成功,默认不修改
    // 获取前端信息
    let { name, tel, isDefault } = req?.body
    // 获取前端传递的user_id
    let { uid } = req?.user
    // 要对isDefault进行判断,如果当前isDefault处于默认,其他所有数据都需要为非默认
    if (isDefault == 1) {
        // 将当前联系人设为默认联系人

        const sql = "update contact set isDefault=0 where user_id=?"
        pool.query(sql, [uid], (err, result) => {
            console.log("------------------1", uid, name, tel, isDefault);
            if (err) {
                console.log("默认联系人错误", err);
                next(err)
                return
            }
            // if (result.affectedRows > 0) {
            // status = true
            // }
            console.log("------------------2", uid, name, tel, isDefault);
            addContact(uid, name, tel, isDefault, next, res)
        })
    } else {
        console.log("------------------3", uid, name, tel, isDefault);
        addContact(uid, name, tel, isDefault, next, res)
    }

})
// 获取默认联系人(1条)
router.get("/get", (req, res, next) => {
    // 获取用户id
    let { uid } = req?.user
    const sql = "select * from contact where user_id = ? and isDefault=1"
    pool.query(sql, [uid], (err, result) => {
        if (err) {
            console.log("获取默认联系人错误", err);
            next(err)
            return
        }
        if (result && result.length > 0) {
            console.log(result);
            // res.send(Response.success({ "name": result[0].name, "tel": result[0].tel }, "获取默认联系人成功!"))
            res.send(Response.success(result, "获取默认联系人成功!"))
        } else {
            res.send(Response.error("获取默认联系人失败!"))
        }
    })
})

// 根据用户id查询联系人接口-返回n条
router.get("/getAll", (req, res, next) => {
    // 获取用户id
    let { uid } = req?.user
    const sql = "select * from contact where user_id = ?"
    pool.query(sql, [uid], (err, result) => {
        if (err) {
            console.log("获取联系人错误", err);
            next(err)
            return
        }
        if (result && result.length > 0) {
            // console.log(result);
            // res.send(Response.success({ "name": result[0].name, "tel": result[0].tel }, "获取默认联系人成功!"))
            res.send(Response.success(result, "获取联系人成功!"))
        } else {
            res.send(Response.error("获取联系人失败!"))
        }
    })
})
// 根据联系人id查询-返回1条
router.get('/getById/:id', (req, res, next) => {
    console.log(req.params);
    let { id } = req?.params
    const sql = "select * from contact where id=?"
    pool.query(sql, [id], (err, result) => {
        if (err) {
            console.log("根据id获取联系人错误", err);
            next(err);
            return
        }
        if (result && result.length > 0) {
            res.send(Response.success(result, "查询成功"))
        } else {
            res.send(Response.error("查询失败"))
        }
    })
})
// 修改联系人接口
router.put('/update', (req, res, next) => {
    // console.log("用户id:", req.user.uid);
    console.log(req.body);
    let { name, tel, id } = req?.body;
    // 编写sql
    const sql = `update contact set name=?,tel=? where id=?`
    pool.query(sql, [name, tel, id], (err, result) => {
        if (err) {
            console.log("修改联系人错误", err);
            next(err);
            return
        }
        console.log(result);
        if (result.changedRows > 0) {
            res.send(Response.success(null, "修改联系人成功"))
        } else {
            res.send(Response.error("修改联系人失败"))
        }
    })

})
// 删除联系人接口
router.delete("/delete/:id", (req, res, next) => {
    let { id } = req?.params
    const sql = "delete from contact where id=?"
    pool.query(sql, [id], (err, result) => {
        if (err) {
            console.log("删除联系人错误", err);
            next(err);
            return
        }
        res.send(Response.success(null, "删除成功"))
    })
})

// 设置默认联系人
router.put("/updateState/:id", (req, res, next) => {
    let { uid } = req?.user
    let { id } = req?.params
    const sql = "update contact set isDefault=0 where user_id=?"
    pool.query(sql, [uid], err => {
        if (err) {
            console.log("统一设置非默认错误", err);
            next(err)
            return
        }
        const sql = "update contact set isDefault=1 where id=?"
        pool.query(sql, [id], (err, result) => {
            if (err) {
                console.log("设置默认错误", err);
                next(err)
                return
            }
            if (result && result.affectedRows > 0) {
                res.send(Response.success(null, "设置默认联系人成功"))
            } else {
                res.send(Response.error("设置默认联系人失败"))
            }
        })
    })
})

// 公共方法:添加联系人
function addContact(uid, name, tel, isDefault, next, res) {
    const sql = "insert into contact(user_id,name,tel,isDefault) values(?,?,?,?)"
    pool.query(sql, [uid, name, tel, isDefault], (err, result) => {
        if (err) {
            console.log("新增联系人错误", err)
            next(err)
            return
        }
        if (result.affectedRows > 0) {
            // console.log(result);
            res.send(Response.success(null, "添加成功!"))
        } else {
            res.send(Response.error("添加失败"))
        }
    })
}
module.exports = router


